import BaseSDK from "./sdk/base/BaseSDK";
import ByteDance from "./sdk/tt/ByteDance";
import WeChat from "./sdk/wechat/WeChat";
import AppConfig from "../config/AppConfig";
import { UpdateUserInfo } from "../gamenet/protobufs/userprotocol/protocol_001";

export enum ePlatformId {
    None = 0,
    Wechat,
    ByteDance,
}

export class Platform {
    /** 单例 */
    static instance: Platform = null;
    /** 平台id */
    public platformId: number = -1;
    /** 平台sdk */
    private _sdk: BaseSDK = new BaseSDK();
    // /** 用户唯一标识(微信登录时前端无法获取openid以保证账户安全) */
    // private _openId: string = ``;

    constructor(){
        Platform.instance = this;
    }
    
    init(platId: ePlatformId) {
        this.platformId = platId;
        switch (platId) {
            case ePlatformId.Wechat: {
                this._sdk = new WeChat();
                break;
            }
            case ePlatformId.ByteDance: {
                this._sdk = new ByteDance();
                break;
            }
        }
    }

    /** 是否是微信平台 */
    getIsWeChat(){
        return cc.sys.platform == cc.sys.WECHAT_GAME && this.platformId == ePlatformId.Wechat;
    }

    /** 获取appid */
    getAppid(){
        return AppConfig.appIdList[this.platformId];
    }

    /** 获取app名称 */
    getAppName(){
        return AppConfig.appName;
    }

    /** 静默登录 */
    silentLogin(): Promise<string>{
        return this._sdk.silentLogin();
    }

    /** 获取用户信息, 如果用户没有授权，则进行授权登录再进行获取用户信息 */
    getUserInfo(): Promise<UpdateUserInfo>{
        return this._sdk.getUserInfo();
    }

    /** 主动转发 */
    shareAppMessage(){
        this._sdk.shareAppMessage();
    }
}